package com.lagou.edu.utils;

import com.lagou.edu.anno.Autowired;
import com.lagou.edu.anno.Componment;

import java.sql.SQLException;

@Componment
public class TransactionManager {

//    private TransactionManager(){
//
//    }
//
//    private static volatile TransactionManager transactionManager = null;
//
//    public static TransactionManager getInstance() {
//        if(null == transactionManager) {
//            synchronized (TransactionManager.class) {
//                if(null == transactionManager) {
//                    transactionManager = new TransactionManager();
//                }
//            }
//        }
//        return transactionManager;
//    }

    @Autowired
    private ConnectionUtils connectionUtils;

    /**
     * 开启事务
     * @throws SQLException
     */
    public void beginTransaction() throws SQLException {
        connectionUtils.getThreadConn().setAutoCommit(false);
    }

    /**
     * 结束事务
     * @throws SQLException
     */
    public void commit() throws SQLException {
        connectionUtils.getThreadConn().commit();
    }

    /**
     * 回滚
     * @throws SQLException
     */
    public void rollback() throws SQLException {
        connectionUtils.getThreadConn().rollback();
    }

}
